<template>
  <div class="login-container">
    <div class="loginCard">
      <div class="login-sidebox">
        <div class="login-sidebox__title">
          <h2>HuoCMS</h2>
          <p>高性能 / 精致 / 优雅</p>
          <p>多语言,多站点,强大的SEO功能</p>
        </div>
        <img src="@/assets/loginbg.png" />
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
        label-position="left">
        <div v-if="authCode">
          <div class="title-container">
            <img src="@/assets/logo.png" alt="" />
            <h2 class="title">请输入短信验证码！</h2>
          </div>
          <el-form-item prop="code">
            <!-- <span class="svg-container">
              <svg-icon icon-class="user" />
            </span> -->
            <el-input ref="code" v-model="code" placeholder="请输入验证码" name="code" type="text" tabindex="1"
              autocomplete="on" />
          </el-form-item>
          <h5 class="desc">
            <span>
              验证码已发送至{{
                phone.replace(phone.substr(3, 4), '****')
              }}，没有收到？
            </span>
            <el-link type="primary" @click="handleLogin('cooldownFlag')">
              <span>再次发送</span>
              <span v-if="cooldown > 0">({{ cooldown }} s)</span>
            </el-link>
            <el-link type="primary" @click="authCode = false" style="margin-left: 10px">
              <span>切换账号</span>
            </el-link>
          </h5>
        </div>
        <div v-else>
          <div class="title-container">
            <img src="@/assets/logo.png" alt="" />
            <h2 class="title">欢迎您登录！</h2>
          </div>
          <el-form-item prop="account">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="account" v-model="loginForm.account" placeholder="用户名" name="account" type="text" tabindex="1"
              autocomplete="on" />
          </el-form-item>
          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                placeholder="密码" name="password" tabindex="2" autocomplete="on" @keyup.native="checkCapslock"
                @blur="capsTooltip = false" @keyup.enter.native="handleLogin" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
        </div>

        <el-button :loading="loading" class="loginBtn" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form>
    </div>

    <!-- <el-dialog title="Or connect with" :visible.sync="showDialog">
      Can not be simulated on local, so please combine you own business simulation! ! !
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog> -->
    <img style="display: none;" src="@/assets/copyright.jpg">
  </div>
</template>

<script>
// import { validaccount } from '@/utils/validate'
// import SocialSign from './components/SocialSignin'
import { getInfo } from '@/api/user'

export default {
  name: 'Login',
  // components: { SocialSign },
  data() {
    return {
      authCode: false,
      phone: '',
      code: '',
      cooldownFlag: false,
      cooldown: 0,
      loginForm: {
        account: '',
        password: ''
      },
      loginRules: {
        account: [
          { required: 'true', message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: 'true', message: '请输入密码', trigger: 'blur' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      publicKey:
        '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5Bxjyw7r9IW08OMudzn0M9CBu3dvKtNOtbMuN7iEQPEosUMbfk3vpi8vX7pdGKHnzUZ47yh44FTcDyoBbOG+NseuTrirkdGCvXyTZyfAZ9TQ/FFBVG1cemoeA5/ry1PMMw6V6EGK8hBL6h/WWZTnksWiWvmLzneQyYphcL1UpKQIDAQAB-----END PUBLIC KEY-----'
    }
  },
  watch: {},

  mounted() {
    if (this.loginForm.account === '') {
      this.$refs.account.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },

  methods: {
    encrypt(str) {
      const jse = new this.$jsEncrypt()
      jse.setPublicKey(this.publicKey)
      return jse.encrypt(str)
    },
    cooldownTime() {
      this.cooldown = 60 // 赋值60秒
      var times = setInterval(() => {
        this.cooldown-- // 递减
        if (this.cooldown <= 0) {
          clearInterval(times)
        }
      }, 1000) // 1000毫秒后执行
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getUserInfo() {
      getInfo().then(res => { })
    },
    handleLogin(flag = '') {
      if (flag == 'cooldownFlag' && this.cooldown > 0) {
        this.$message.warning('请在' + this.cooldown + '秒之后再发送验证码！')
        return
      }

      if (flag != 'cooldownFlag' && this.authCode) {
        this.checkCode()
        return
      }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // 账号密码加密

          const jse = new this.$jsEncrypt()
          jse.setPublicKey(this.publicKey)
          const data = {
            account: this.encrypt(this.loginForm.account),
            password: this.encrypt(this.loginForm.password)
          }

          this.loading = true
          this.$store
            .dispatch('user/login', data)
            .then(res => {
              if (res.msg == 'code_auth') {
                this.authCode = true
                this.phone = res.data.phone
                this.loading = false
                if (flag == 'cooldownFlag') {
                  this.cooldownTime()
                }
                return
              }
              if (res.data.token) {
                this.$router.push({
                  path: this.redirect || '/',
                  query: this.otherQuery
                })
                this.$message.success(res.msg)
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    checkCode() {
      this.loading = true

      const codeForm = {
        phone: this.encrypt(this.phone),
        code: this.encrypt(this.code)
      }

      this.$store
        .dispatch('user/authCode', codeForm)
        .then(res => {
          if (res.data.token) {
            this.$router.push({
              path: this.redirect || '/',
              query: this.otherQuery
            })
            this.loading = false
            this.$message.success(res.msg)
          } else {
            this.loading = false
            this.code = ''
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
#app .el-button.loginBtn {
  margin-top: 30px;
  border-radius: 0;
  border-color: #f83600;
}

#app .el-button.loginBtn:active {
  border-color: #f83600;
}

/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: #000;
  }
}

/* reset element-ui css */
.login-container .el-form-item {
  border: none !important;
  border-bottom: solid 1px #ddd !important;
}

.login-container {
  height: 100vh;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    // input:-internal-autofill-selected{
    //   background-color: #fff !important;
    // }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;

      &:-webkit-autofill {
        // 自动填充input样式
        // box-shadow: 0 0 0px 1000px #000 inset !important;
        -webkit-text-fill-color: #000 !important;
        background-color: #fff;
      }
    }
  }

  el-link {
    display: inline-block;
    vertical-align: middle;
  }

  span {
    display: inline-block;
    vertical-align: middle;
  }

  .desc {
    width: 80%;
    margin: 0 auto;
    vertical-align: middle;
  }

  .el-form-item {
    width: 80%;
    margin: 0 auto;
    margin-bottom: 20px;
    border: 1px solid #ddd;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__content {
    line-height: 33px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #fff;
  overflow: hidden;
  position: relative;

  .loginCard {
    width: 85%;
    max-width: 1030px;
    height: 670px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    justify-content: space-between;
    box-shadow: 0px 0px 60px 0px rgba(0, 0, 0, 0.08);
  }

  .login-sidebox {
    width: 50%;
    position: relative;
    overflow: hidden;

    .login-sidebox__title {
      margin: 220px 0 20px 61px;
      color: #fff;

      h2 {
        font-size: 52px;
        letter-spacing: 3px;
        margin-bottom: 6px;
        font-family: Arial;
      }

      h4 {
        line-height: 50px;
        font-weight: normal;
      }

      p {
        font-size: 17px;
        font-weight: normal;
        font-family: MicrosoftYaHeiLight;
        line-height: 24px;
        letter-spacing: 1px;
      }
    }

    img {
      position: absolute;
      top: 0;
      width: 520px;
      height: 670px;
      z-index: -1;
    }
  }

  .login-form {
    width: 50%;
    max-width: 100%;
    padding: 105px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #000;

    // margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    img {
      margin: 0 auto;
      margin-bottom: 20px;
      display: block;
    }

    .title {
      font-size: 26px;
      color: #333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #000;
    cursor: pointer;
    user-select: none;
  }

  .loginBtn {
    width: 80%;
    margin: 0 auto;
    display: block;
    background-color: #f83600;
    color: #fff;
    border-radius: 0;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

  @media screen and (max-width: 1000px) {
    .login-form {
      width: 100%;
    }

    .login-sidebox {
      display: none;
    }
  }
}
</style>
